package com.yang.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.domain.Result;
import com.yang.domain.VO.LinkVo;
import com.yang.domain.VO.PageVo;
import com.yang.domain.entity.Link;
import com.yang.mapper.LinkMapper;
import com.yang.service.LinkService;
import com.yang.utils.BeanCopyUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

import static com.yang.constants.SystemConstants.LINK_STATUS_NORMAL;



/**
 * <p>
 * 友链(Link)表服务实现类
 * </p>
 * @author yang
 * @date 2023/5/29
 */
@Service
public class LinkServiceImpl extends ServiceImpl<LinkMapper, Link> implements LinkService {


    @Override
    public Result getAllLink() {

        //1.查询出所有审核通过的友链
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Link::getStatus,LINK_STATUS_NORMAL);
        List<Link> linkList = list(queryWrapper);

        //2.linkList-->linkVoList
        List<LinkVo> linkVos = BeanCopyUtils.copyBeanList(linkList, LinkVo.class);

        //3.返回vo类
        return Result.okResult(linkVos);
    }

    @Override
    public Result finAllLinkList(Link link, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.hasText(link.getName()),Link::getName, link.getName());
        queryWrapper.eq(Objects.nonNull(link.getStatus()),Link::getStatus, link.getStatus());

        Page<Link> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);

        //转换成VO
        List<Link> categories = page.getRecords();
        PageVo pageVo = new PageVo();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(categories);

        return Result.okResult(pageVo);
    }

    @Override
    public Result addLink(Link link) {
        save(link);
        return Result.okResult();
    }

    @Override
    public Result editLink(Link link) {
        updateById(link);
        return Result.okResult();
    }

    @Override
    public Result removeLink(Long id) {
        removeById(id);
        return Result.okResult();
    }
}
